package com.settlement.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.settlement.system.common.enums.FieldNameEnum;
import com.settlement.system.common.enums.ReportValueEnum;
import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.enums.SendReturnConditionEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.common.util.common.CacheUtil;
import com.settlement.system.common.util.common.EnumUtil;
import com.settlement.system.common.util.common.RedisUtil;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import com.settlement.system.model.entity.common.TblWpSendReturnSqlConfig;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import com.settlement.system.service.ITblWpSendReturnSqlConfigService;
import java.util.Map;

/**
 * 发退货字段配置表 前端控制器
 * @author hc
 * @since 2022-07-26
 */

@Api(tags = "发退货字段配置表",description = "发退货字段配置表")
@RestController
@RequestMapping("/wp/sendReturnSqlConfig")
@Slf4j
public class TblWpSendReturnSqlConfigController extends BaseController<TblWpSendReturnSqlConfig> {

    @Resource
    private ITblWpSendReturnSqlConfigService tblWpSendReturnSqlConfigService;

    @Override
    public IBaseService init() {
        return tblWpSendReturnSqlConfigService;
    }

    @ApiOperation("获取字段列表")
    @RequestMapping(value = "getFieldNameList", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse getFieldNameList() {
        Map<String, Object> resultMap = Maps.newHashMap();
        try {
            resultMap.put("rows", EnumUtil.toList(FieldNameEnum.class));
            return new ObjectDataResponse<Map>(resultMap, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception e) {
            log.error("获取字段列表异常", e);
            return new ObjectDataResponse<Map>(null, ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @ApiOperation("获取报表取值列表")
    @RequestMapping(value = "getReportValueList", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse getReportValueList() {
        Map<String, Object> resultMap = Maps.newHashMap();
        try {
            resultMap.put("rows", EnumUtil.toList(ReportValueEnum.class));
            return new ObjectDataResponse<Map>(resultMap, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception e) {
            log.error("获取报表取值列表异常", e);
            return new ObjectDataResponse<Map>(null, ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @ApiOperation("获取报表条件列表")
    @RequestMapping(value = "pullColumnNameList", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse pullColumnNameList() {
        Map<String, Object> resultMap = Maps.newHashMap();
        try {
            resultMap.put("rows", EnumUtil.toList(SendReturnConditionEnum.class));
            return new ObjectDataResponse<Map>(resultMap, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception e) {
            log.error("获取报表条件列表异常", e);
            return new ObjectDataResponse<Map>(null, ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @Override
    protected void setModelValue(TblWpSendReturnSqlConfig model) throws Exception {
        tblWpSendReturnSqlConfigService.convertSendReturnSql(model);
    }

    @Override
    protected ObjectDataResponse delete(@RequestBody TblWpSendReturnSqlConfig model, HttpServletRequest request) throws ServiceException {
        return super.delete(model, request);
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblWpSendReturnSqlConfig model, HttpServletRequest request) throws ServiceException {
        Map<String,Object> params=Maps.newHashMap();
        params.put("fieldName",model.getFieldName());
        Integer count = tblWpSendReturnSqlConfigService.selectCount(params);
        if(count>0){
            return new ObjectDataResponse(500, "新增失败，字段：" + model.getFieldName()+"，存在条件重复数据。");
        }
        return super.insert(model, request);
    }

    @Override
    protected ObjectDataResponse update(@RequestBody TblWpSendReturnSqlConfig model, HttpServletRequest request) throws ServiceException {
        Map<String,Object> params=Maps.newHashMap();
        params.put("fieldName",model.getFieldName());
        TblWpSendReturnSqlConfig sqlConfig=new TblWpSendReturnSqlConfig();
        sqlConfig.setFieldName(model.getFieldName());
        sqlConfig = tblWpSendReturnSqlConfigService.selectSingleByModel(sqlConfig);
        if(ObjectUtil.isNotNull(sqlConfig)&& !sqlConfig.getId().equals(model.getId())){
            return new ObjectDataResponse(500, "修改失败，字段：" + model.getFieldName()+"，存在条件重复数据。");
        }
        return super.update(model, request);
    }
}

